package com.nepenthe.view;

import com.nepenthe.Utils.MybatisUtils;
import com.nepenthe.Utils.UserGuiUtils;
import com.nepenthe.dao.UserMapper;
import com.nepenthe.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import java.awt.*;

public class register {
    public void registerInit() {
        JFrame jFrame = UserGuiUtils.userGuiInit();
        //初始化--往窗体里放其他控件
        init(jFrame);
        //设置窗体可见
        jFrame.setVisible(true);
    }

    public void init(JFrame jFrame) {
        //标题栏
        JPanel titlePanel = new JPanel();
        JLabel registerLabel = new JLabel("注册");
        titlePanel.add(registerLabel);
        jFrame.add(titlePanel, "North");

        /*
          信息栏
          姓名
          密码
          身份证号
          学号
         */
        JLabel usernameLabel = new JLabel("姓名");
        JTextField usernameField = new JTextField();
        //密码
        JLabel passwordLabel = new JLabel("密码");
        JPasswordField pwdField = new JPasswordField();
        //身份证号
        JLabel idCardLabel = new JLabel("身份证号");
        JTextField idCardField = new JTextField();
        //学号
        JLabel studentLabel = new JLabel("学号");
        JTextField studentField = new JTextField();
        JPanel userInfo = new JPanel();
        userInfo.setLayout(null);
        //定位
        usernameLabel.setBounds(50, 20, 50, 20);
        passwordLabel.setBounds(50, 60, 50, 20);
        idCardLabel.setBounds(50, 100, 50, 20);
        studentLabel.setBounds(50, 140, 50, 20);

        usernameField.setBounds(110, 20, 120, 20);
        pwdField.setBounds(110, 60, 120, 20);
        idCardField.setBounds(110, 100, 120, 20);
        studentField.setBounds(110, 140, 120, 20);

        //添加到窗体
        userInfo.add(usernameLabel);
        userInfo.add(usernameField);
        userInfo.add(passwordLabel);
        userInfo.add(pwdField);
        userInfo.add(idCardLabel);
        userInfo.add(idCardField);
        userInfo.add(studentLabel);
        userInfo.add(studentField);
        jFrame.add(userInfo, "Center");

        //注册按钮及提示信息
        JPanel btns = new JPanel();
        btns.setLayout(new GridLayout(2, 1));
        JTextField jTextField = new JTextField();
        JButton registerBtn = new JButton("注册");
        registerBtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));

        btns.add(jTextField, "North");
        btns.add(registerBtn, "South");
        jFrame.add(btns, "South");
        //注册按钮事件
        registerBtn.addActionListener(e -> {
            //获取mapper对象
            SqlSession sqlSession = MybatisUtils.getsqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User user = new User();
            user.setName(usernameField.getText());
            user.setPwd(String.valueOf(pwdField.getPassword()));
            user.setIdCard(idCardField.getText());
            user.setStudentID(studentField.getText());
            if (e.getSource() == registerBtn) {
                if (!"".equals(usernameField.getText())
                        && !"".equals(String.valueOf(pwdField.getPassword()))
                        && !"".equals(idCardField.getText())
                        && !"".equals(studentField.getText())
                ) {
                    //写入到数据库
                    mapper.addUser(user);
                    sqlSession.commit();
                    sqlSession.close();
                    System.out.println("注册成功");
                    //跳转到初始页面
                    jFrame.dispose();
                    new index().indexInit();
                } else {
                    jTextField.setText("信息不完整哟～\n请重新输入");
                }
            }
        });
    }
}
